//
//  HuatiViewController.m
//  BlueTDevice
//
//  Created by MingmingSun on 2016/11/5.
//  Copyright © 2016年 Sunmingming. All rights reserved.
//

#import "HuatiViewController.h"
#import "FTCTGiraffeExampleViewController.h"

@interface HuatiViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,weak) IBOutlet UITableView *tableView;
@property(nonatomic,strong) NSDictionary *huatiData;

@end

@implementation HuatiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.huatiData = nil;
    [self startSearch];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 150.0f;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.huatiData?2:0;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = nil;
    static NSString *inde = @"huatiid";
    cell = [tableView dequeueReusableCellWithIdentifier:inde];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"HuatiCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    NSDictionary *child = self.huatiData[[NSString stringWithFormat:@"%d",indexPath.row]];
    UILabel *lab1 = (UILabel*)[cell viewWithTag:1];
    UILabel *lab2 = (UILabel*)[cell viewWithTag:2];
    UILabel *lab3 = (UILabel*)[cell viewWithTag:3];
    UILabel *lab4 = (UILabel*)[cell viewWithTag:4];
    
    lab1.text = child[@"title"];
    lab2.text = child[@"abstract"];
    lab3.text = child[@"author"];
    lab4.text = [NSString stringWithFormat:@"赞%@ 分享%@",child[@"good"],child[@"share"]];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    FTCTGiraffeExampleViewController *vc = [FTCTGiraffeExampleViewController new];
    vc.selectID = indexPath.row;
    vc.title = self.huatiData[[NSString stringWithFormat:@"%d",indexPath.row]][@"title"];
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)rightPressed:(id)sender{
    [self startSearch];
}

-(void)startSearch{
    [SVProgressHUD showWithStatus:@"loading..."];
    [APPALL.netSession GET:[NSString stringWithFormat:@"%@%@",APIURL,APIAbstracts]
                 parameters:nil
                   progress:nil
                    success:^(NSURLSessionDataTask * task, id responseObject){
                        NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
                        NSLog(@"JSON: %@", JSON);
                        if([JSON[@"get_abstract"] isEqualToString:@"success"]){
                            self.huatiData = JSON;
                            [SVProgressHUD dismiss];
                            [self.tableView reloadData];
                        }else{
                            [SVProgressHUD dismiss];
                            //[SVProgressHUD showErrorWithStatus:@"获取话题失败"];
                        }
                    }
                    failure:^(NSURLSessionDataTask * task, NSError * error){
                        [SVProgressHUD dismiss];
                        //[SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"请求失败:%@", error.description]];
                    }];
}

@end
